extends Node

const SHIP_DATA_PATH = "res://data/Ships.json"
const CARD_DATA_PATH = "res://data/Cards.json"
const CARD_BASE = preload("res://scenes/baseScenes/card.tscn")
const BOSS_01 = preload("res://scenes/enemies/boss_01_.tscn")

@onready var ammo: Node2D = $Ammo
@onready var enemies: Node2D = $Enemies
@onready var items: Node2D = $Items
@onready var game_stats: GameStats = $GameStats
@onready var menu_ui: Control = $MenuUI
@onready var warning_screen: Control = $WarningScreen
@onready var marker_2d: Marker2D = $Marker2D

var stop_count:int = 0:
	set(v):
		stop_count = max(0, v)
		if stop_count == 0:
			get_tree().paused = false
		else:
			get_tree().paused = true
var travel_mileage: int = 0:
	set(v):
		travel_mileage = v
		if travel_mileage > 3600 and not boss_battle:
			boss_battle = true
			start_boss_battle()

var enemy_create: bool = true
var boss_battle: bool = false

var inventory := Inventory.new()

func _ready() -> void:
	#捕获鼠标
	#Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	
	pass

#创建子弹
func instance_ammo(node, location, parent, rota, creator):
	var ammo_node = node.instantiate()
	ammo_node.global_position = location
	ammo_node.global_rotation = rota
	ammo_node.ammo_owner = creator
	parent.add_child(ammo_node)
	return ammo_node

#创建node对象，如敌机
func instance_node(node, location, parent, rota):
	var instance_nodes = node.instantiate()
	instance_nodes.global_position = location
	instance_nodes.global_rotation = rota
	parent.add_child(instance_nodes)
	return instance_nodes

#创建card对象
func instance_card(card_base, parent, card_id):
	var card = card_base.instantiate()
	card.CardID = card_id
	parent.add_child(card)
	return card

#计算伤害
func calculate_damage(attacker: Node2D, attackered: Node2D) -> float:
	
	#计算敌人对玩家的伤害
	if attacker.is_in_group("enemies"):
		if attacker is Ammo:
			return (attacker.ammo_owner.stats.attack_damage + game_stats.attack_number_addition - attackered.stats.defensive_power) * game_stats.attack_magnification
		return attacker.stats.attack_damage - attackered.stats.defensive_power
	
	#计算玩家对敌人的伤害
	if attacker is BaseShip or attacker.ammo_owner is BaseShip:
		return (attacker.ammo_owner.stats.attack_damage + game_stats.attack_number_addition - attackered.stats.defensive_power) * game_stats.attack_magnification
	
	#如果有问题就返回0
	return 0

#加载JSON文件
func load_json_file(file_path: String):
	if not FileAccess.file_exists(file_path):
		print(file_path + "不存在")
		return
	var file = FileAccess.open(file_path, FileAccess.READ)
	var data = JSON.parse_string(file.get_as_text()) as Dictionary
	return data

#初始化飞船的Stats
func initialize_stats(ship: CharacterBody2D) -> void:
	var data = load_json_file(SHIP_DATA_PATH).data
	for ship_data in data:
		if ship_data.id == ship.name:
			ship.stats.max_energy_shield = ship_data.max_energy_shield
			ship.stats.max_structural_strength = ship_data.max_structural_strength
			ship.stats.max_energy = ship_data.max_energy
			ship.stats.shot_wait_time = ship_data.shot_wait_time
			ship.stats.transmit_wait_time = ship_data.transmit_wait_time
			ship.stats.running_speed = ship_data.running_speed
			ship.stats.transmit_speed = ship_data.transmit_speed
			ship.stats.transmit_duration = ship_data.transmit_duration
			ship.stats.transmit_cost = ship_data.transmit_cost
			ship.stats.attack_damage = ship_data.attack_damage
			ship.stats.defensive_power = ship_data.defensive_power
			
			ship.stats.energy_shield_regen = ship_data.energy_shield_regen
			ship.stats.structural_strength_regen = ship_data.structural_strength_regen
			ship.stats.energy_regen = ship_data.energy_regen

#飞船拾取item时的处理
func on_item_picked_up(_picker: BaseShip, pickup: PickUp) -> void:
	if pickup is Experience:
		game_stats.ship_experience += 1

#暂停游戏
func stop_game() -> void:
	stop_count += 1
	Input.mouse_mode = Input.MOUSE_MODE_VISIBLE

#继续游戏
func continue_game() -> void:
	stop_count -= 1
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

#升级时的处理
func _on_game_stats_level_up() -> void:
	var card_parent = menu_ui.h_card_box
	for child in card_parent.get_children():
		child.queue_free()
	
	var cards_data = Game.load_json_file(Game.CARD_DATA_PATH)
	var cards_data_keys = cards_data.keys()
	
	#随机生成卡片，目前还有待改进
	for i in 3:
		instance_card(CARD_BASE, card_parent, cards_data_keys[randi_range(0, cards_data_keys.size() - 1)])
	
	stop_game()
	menu_ui.is_level_up_pause = true
	menu_ui.open_menu()

#选择完升级的奖励卡片
func finish_card_choose() -> void:
	menu_ui.is_level_up_pause = false
	menu_ui.card_choose.hide()
	continue_game()
	menu_ui.close_menu()

#boss战开启
func start_boss_battle() -> void:
	warning_screen.display()
	enemy_create = false

#boss战结束
func finish_boss_battle() -> void:
	travel_mileage = 0
	boss_battle = false
	enemy_create = true

#警告播放完毕
func _on_warning_screen_warning_finished() -> void:
	instance_node(BOSS_01, marker_2d.global_position, enemies, enemies.rotation)
